package reliability.ui;

import reliability.model.GraphModel;
import reliability.model.GraphModelEvent;
import reliability.model.GraphModelListener;
import reliability.types.graph.NetworkElement;
import reliability.types.graph.Node;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class CurrentElementPanel
    extends InfoPanelComponent
    implements PropertyChangeListener
{
    private static final String NO_SELECTED_ITEM_LABEL = "Текущий элемент графа не выбран";
    private static final String NODE_SELECTED_LABEL = "Текущий элемент графа: узел графа №";
    private static final String EDGE_SELECTED_LABEL = "Текущий элемент графа: ребро графа №";
    private static final Color ENABLED_FIELD_COLOR = new Color(255, 255, 255);
    private static final Color DISABLED_FIELD_COLOR = new Color(240, 240, 240);

    private JLabel currentElementLabel;
    private NumericField currentElementField;

    public CurrentElementPanel(GraphModel model)
    {
        super(model);
        setPreferredSize(new Dimension(290, 80));
        setBorder(BorderFactory.createTitledBorder("Текущий элемент графа"));
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);
        setLayout(layout);

        currentElementLabel = new JLabel();
        currentElementLabel.setFont(InfoPanel.DEFAULT_FONT);
        add(currentElementLabel);

        currentElementField = new NumericField(23);
        currentElementField.setFont(InfoPanel.DEFAULT_FONT);
        currentElementField.addPropertyChangeListener("value", this);
        currentElementField.addFocusListener(this);
        disableTextField();
        add(currentElementField);
    }

    public void graphModelChanged(GraphModelEvent event)
    {
        switch (event.getId())
        {
            case GraphModelEvent.SELECTION_CHANGED:
                onGraphModelSelectionChanged();
                break;
            case GraphModelEvent.GRAPH_INIT:
                disableTextField();
                break;
        }
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        double reliability = getDoubleValue(currentElementField.getValue());
        NetworkElement selectedItem = model.getSelectedItem();
        selectedItem.setReliability(reliability);
    }

    private void onGraphModelSelectionChanged()
    {
        NetworkElement selectedItem = model.getSelectedItem();
        if (selectedItem != null)
        {
            String labelTextHead = (selectedItem instanceof Node) ? NODE_SELECTED_LABEL : EDGE_SELECTED_LABEL;
            String keyStr = model.getSelectedItemKey().toString();
            currentElementLabel.setText(labelTextHead + keyStr);
            enableTextField(selectedItem.getReliability());
        }
        else
        {
            disableTextField();
        }
    }

    private void enableTextField(double newValue)
    {
        currentElementField.setEnabled(true);
        currentElementField.setBackground(ENABLED_FIELD_COLOR);
        currentElementField.setValue(newValue);
    }

    private void disableTextField()
    {
        currentElementLabel.setText(NO_SELECTED_ITEM_LABEL);
        currentElementField.setEnabled(false);
        currentElementField.setText("");
        currentElementField.setBackground(DISABLED_FIELD_COLOR);
    }
}
